package com.neusoft.training.podcast.controller;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.training.podcast.entity.Comment;
import com.neusoft.training.podcast.entity.User;
import com.neusoft.training.podcast.service.CommentService;
import com.neusoft.training.podcast.service.UserService;

@Controller
public class CommentController {
	
	@Autowired
	private CommentService commentService;
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/commentList")
	public ModelAndView comments(ModelAndView mv) {
		List<Comment> comments = commentService.findAll();
		List<String>  userNames = new ArrayList();
		Integer userID;
		for (int i=0; i< comments.size();i++) {
			userID = comments.get(i).getCommentUserID();
			userNames.add(userService.findByID(userID).getName());
		}
		
		mv.addObject("comments", comments);
		mv.addObject("userNames", userNames);
		mv.setViewName("commentMg");
		return mv;

	} 
	
	@RequestMapping("/searchComment")
	public String searchComment(){
		return "searchComment";
	}
	
	@RequestMapping("/searchCommentResult")
	public void result(Integer commentID)
	{
		commentService.deleteComment(commentID);
	}
	
	@RequestMapping("/deleteComment")
	public @ResponseBody String deleteComment(Integer commentID){
		
		commentService.deleteComment(commentID);
		
		return "delete_success";
		
	}
	
	/*
	 * 2018.3.18
	 * @author Cindy*/
	// 添加评论
	@RequestMapping("/addComment")
	public String addComment(){
		
		return "addComment";
	}
	
	@RequestMapping("/saveComment")
	@ResponseBody
	public String saveComment(Integer commentUserID,
			String commentVideoID,
			String context,
			String myCommentTime,
			Integer addCommentID){
		Timestamp commentTime = Timestamp.valueOf(myCommentTime);
		//System.out.println(commentTime);
		commentService.insert(commentUserID, commentVideoID, context, commentTime, addCommentID, 0);
		System.out.println("save success");
		
		return "success";
	}
	
	// 根据视频ID查找一级评论
	@RequestMapping("/searchCommentByVideoID")
	public String searchCommentByVideoID(){
		return "searchCommentByVideoID";
	}
	
	@RequestMapping("/resultByVideoID")
	public @ResponseBody List<Comment> resultByVideoID(String commentVideoID){
		
		List<Comment> comments = commentService.findCommendByVideoID(commentVideoID);
		//System.out.println(comments.size());
		//System.out.println(commentVideoID);
		return comments;
	}
	
	// 查找二级评论
	@RequestMapping("/findSecondaryComment")
	public @ResponseBody List<Comment> findSecondaryComment(String commentVideoID,Integer commentID){
		List<Comment> comments = commentService.findSecondaryComment(commentVideoID, commentID);
		return comments;
	}

	
}
